<?php
namespace app\appa\controller;

use app\appa\model\CodeModel;
use app\appa\model\MembersModel;
use app\appa\validate\MemberValidate;
use app\common\controller\Api;

class Login extends Api
{
    //注册
    public function login()
    {
//        echo md5(1231231234);
        $data = input('post.');
        //验证数据
        if (empty($data['mobile'])){
            $this->error('手机号不能为空');
        }
        if (empty($data['password'])){
            $this->error('密码不能为空');
        }
        $validate = new MemberValidate();
        $result = $validate->scene('login')->check($data);
        if (!$result){
            return error($validate->getError());
        }
        $user = MembersModel::where('mobile',$data['mobile'])->find();
        if (!$user){
            $this->error('该账号未注册');
        }
        if (md5($data['password'].$user['salt']) != $user['password']){
            $this->error('密码错误');
        }
        MembersModel::update([
           'last_login_time' => date('Y-m-d H:i:s'),
           'last_login_ip' => request()->ip()
        ],['mobile' => $data['mobile']]);
        $this->success('登录成功',[
            'token' => singToken($user['id'],$user['mobile'])
        ]);
    }

    //注册
    public function register()
    {
        $data = input('post.');
        //验证数据
        if (empty($data['mobile'])){
            $this->error('手机号不能为空');
        }
        if (empty($data['password'])){
            $this->error('密码不能为空');
        }
        $validate = new MemberValidate();
        $result = $validate->scene('login')->check($data);
        if (!$result){
            return error($validate->getError());
        }
        $user = MembersModel::where('mobile',$data['mobile'])->find();
        if ($user){
            $this->error('重复使用手机号注册');
        }
        $r = checkSmsCode($data['mobile'],$data['code']);
        if(!$r){
            return error('验证码验证失败');
        }
//        $code = CodeModel::where('mobile',$data['mobile'])->find();
//        if ($data['code'] != $code['code']){
//            $this->error('验证码不正确');
//        }
        // 处理密码
        $randStr = createRandStr(4);
        $newPassword = md5($data['password'].$randStr);
//        $data['salt'] = $randStr;
//        $data['password'] = $newPassword;
//        $data['status'] = 'opt0';
//        $data['name'] = $data['mobile'];
        $data = [
            'salt' => $randStr,
            'password' => $newPassword,
            'status' => 'opt0',
//            'send_time' => time(),
            'name' => $data['mobile'],
            'mobile' => $data['mobile']
        ];
        if (MembersModel::create($data)){
            $this->success('注册成功');
        }else{
            $this->error('注册失败');
        }
    }


    //验证码
    public function Sms()
    {
        $mobile = input('mobile/d');
        if (empty($mobile)){
            $this->error('请填手机号');
        }
        $yzm = createRandStr(4);
        $data = [
            'mobile' => $mobile,
            'code' => $yzm,
            'send_time' => time()
        ];
        $add = CodeModel::create($data);
        if ($add){
            $this->success('发送成功',$yzm);
        }
        $this->error('发送失败');
    }

    //忘记密码
    public function forgot()
    {
        $data = input('post.');
        //验证数据
        if (empty($data['mobile'])){
            $this->error('手机号不能为空');
        }
        if (empty($data['password'])){
            $this->error('密码不能为空');
        }
        $validate = new MemberValidate();
        $result = $validate->scene('login')->check($data);
        if (!$result){
            return error($validate->getError());
        }
        $user = MembersModel::where('mobile',$data['mobile'])->find();
        if (!$user){
            $this->error('该手机号未注册');
        }
        $r = checkSmsCode($data['mobile'],$data['code']);
        if(!$r){
            return error('验证码验证失败');
        }
        $randStr = createRandStr(4);
        $newPassword = md5($data['password'].$randStr);

        $user->salt = $randStr;
        $user->password = $newPassword;
//        $user->send_time => time();
        if ($user->save()) {
            $this->success('修改成功');
        }
        $this->error('修改失败');


    }


}